<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios 发送 AJAX 请求</title>
    <!-- crossorigin： 发送请求时不会携带当前域名下的cookie-->
    <link crossorigin="anonymous" href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
        rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
</head>


<body>
    <div class="container">
        <h2 class="page-header">Axios发送AJAX请求</h2>
        <button class="btn btn-primary">GET</button>
        <button class="btn btn-danger">POST</button>
        <button class="btn btn-info">通用型方法axios</button>
    </div>
</body>
<script>
    const BASE_URL = "http://localhost:8000"
    const btns = document.querySelectorAll("button");
    axios.defaults.baseURL = BASE_URL
    btns[0].onclick = function () {
        axios.get('/axios-server', {
            //url
            params: {
                name: "Tom",
                age: 12
            },
            //headers
            headers: {
                name: "collins",
                address: "home"
            }
        }).then(value => {
            console.log(value);
        })
    };
    btns[1].addEventListener('click', () => {
        axios.post('/axios-server',
            // data-payload
            {
                username: "admin",
                password: "admin"
            },
            {
                //url params
                params: {
                    name: "Tom",
                    age: 12
                },
                //headers
                headers: {
                    name: "collins",
                    address: "home"
                }
            }).then(value => {
                console.log(value);
            })
    });
    btns[2].addEventListener('click', () => {
        axios({
            method: 'post',
            url: '/axios-server',
            // url params
            params: {
                name: "Lucy",
                age: 18
            },
            // headers
            headers: {
                a: 100,
                b: 200
            },
            // payload body
            data: {
                username: "admin",
                password: "admin"
            }
        }).then(response=>{
            // response line - status
            console.log(response.status);
            // response line - status text
            console.log(response.statusText);
            // response header
            console.log(response.headers);
            // response data
            console.log(response.data);
            // response line - response
            console.log(response);
        })
    });
</script>